package com.briup;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @Auther: xiongwenbin
 * @Date: 2024/7/25-07-25-下午4:58
 * @Description: com.briup
 */
public class ConfigurationImpl {
    private static Map map=new HashMap();
    private static Properties properties=new Properties();
    private static ConfigurationImpl configuration=new ConfigurationImpl();
    private ConfigurationImpl(){
    };
    static  {
        SAXReader reader =new SAXReader();
        Document document = null;
        try {
            document = reader.read(ConfigurationImpl.class.getClassLoader().getResourceAsStream("Config.xml"));
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
        Element root=document.getRootElement();

        root.elements().forEach(element -> {
            try {
                String aClass = element.attribute("class").getValue();
                System.out.println( Class.forName(aClass).newInstance());
                map.put(element.getName(), Class.forName(aClass).newInstance());
                element.elements().forEach(e -> properties.put(e.getName(), e.getText()));
            }catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
    public ConfigurationImpl loadConfig() throws DocumentException {
        return configuration;
    }
    public static Map getMap() {
        return map;
    }
    public static Properties getProperties() {
        return properties;
    }
}
